/**
 * created by 小卷毛, 2019/7/22
 * Copyright (c) 2019, 416143467@qq.com All Rights Reserved.
 * #                   *********                            #
 * #                  ************                          #
 * #                  *************                         #
 * #                 **  ***********                        #
 * #                ***  ****** *****                       #
 * #                *** *******   ****                      #
 * #               ***  ********** ****                     #
 * #              ****  *********** ****                    #
 * #            *****   ***********  *****                  #
 * #           ******   *** ********   *****                #
 * #           *****   ***   ********   ******              #
 * #          ******   ***  ***********   ******            #
 * #         ******   **** **************  ******           #
 * #        *******  ********************* *******          #
 * #        *******  ******************************         #
 * #       *******  ****** ***************** *******        #
 * #       *******  ****** ****** *********   ******        #
 * #       *******    **  ******   ******     ******        #
 * #       *******        ******    *****     *****         #
 * #        ******        *****     *****     ****          #
 * #         *****        ****      *****     ***           #
 * #          *****       ***        ***      *             #
 * #            **       ****        ****                   #
 */
package com.zhiyou.aifeng.common.event

import android.content.Context
import android.widget.Toast
import androidx.lifecycle.LifecycleOwner
import androidx.lifecycle.Observer

interface IBaseViewEventObserver : IBaseViewEvent {

    fun getIContext(): Context

    fun getLifecycleOwner(): LifecycleOwner

    fun getViewModel(): BaseViewEvent? = null

    fun getViewModelList(): MutableList<BaseViewEvent> = mutableListOf()

    fun initViewModelEvent() {
        val list: MutableList<BaseViewEvent> = mutableListOf()
        val initViewModelList = getViewModelList()

        if (initViewModelList.isEmpty()) {
            val baseViewModel = getViewModel()
            baseViewModel?.let { list.add(it) }
        } else {
            list.addAll(initViewModelList)
        }

        if (list.isNotEmpty()) observeEvent(list)
    }

    fun observeEvent(viewModelList: MutableList<BaseViewEvent>) {
        viewModelList.forEach {
            it.baseActionEvent.observe(
                getLifecycleOwner(),
                Observer { event ->
                    when (event.action) {
                        BaseEvent.SHOW_LOADING -> showLoading(event.message)
                        BaseEvent.DISMISS_LOADING -> dismissLoading()
                        BaseEvent.TOAST -> showToast(event.message)
                        BaseEvent.FINISH -> finishView()
                    }
                }
            )
        }
    }

    override fun showToast(msg: String) {
        Toast.makeText(getIContext(), msg, Toast.LENGTH_SHORT).show()
    }

}